{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit FormatPrefs;

interface

uses Classes, XMLHelper;

const
  PREFS_FILENAME = 'Preferences.xml';

  DEF_EXTNS_VIDEO = '".AVI",".MPG",".MPEG",".WMV"';
  DEF_EXTNS_AUDIO = '".MP3",".WAV",".CDA",".WMA",".MID",".MIDI"';

type
  // This is a stripped down version of Preferences class to read formats only
  TAHMFormatPreferences = class
  private
    FVideoFormats: TStringList;
    FAudioFormats: TStringList;
    FHelper: TAHMXMLHelper;
  protected
    function ParseXML(const Filepath: String): Boolean;
    function CheckVideoFormat(Extension: String): Boolean;
    function CheckAudioFormat(Extension: String): Boolean;
  public
    constructor Create;
    destructor Destroy; override;
    class procedure LoadFormats(const Filepath: String);
  end;

implementation

uses SysUtils, AHMConsts, TagNames, XMLTags, TagParser, InternetHelper;

constructor TAHMFormatPreferences.Create;
begin
  inherited;

  FVideoFormats := TStringList.Create;
  FAudioFormats := TStringList.Create;

  // Set default formats
  FVideoFormats.CommaText := DEF_EXTNS_VIDEO;
  FAudioFormats.CommaText := DEF_EXTNS_AUDIO;
end;

destructor TAHMFormatPreferences.Destroy;
begin
  FVideoFormats.Free;
  FAudioFormats.Free;

  inherited;
end;

class procedure TAHMFormatPreferences.LoadFormats(const Filepath: String);
begin
  // Parse XML to determine required media formats
  with TAHMFormatPreferences.Create do
  try
    try
      ParseXML(Filepath);
      TagParser.IncludeMP3Tags := CheckAudioFormat(EXT_MP3);
      TagParser.IncludeM4ATags := CheckAudioFormat(EXT_M4A);
      TagParser.IncludeWMATags := CheckAudioFormat(EXT_WMA);
      TagParser.IncludeAVITags := CheckVideoFormat(EXT_AVI);
      TagParser.IncludeMPGTags := CheckVideoFormat(EXT_MPG);
      TagParser.IncludeMPEGTags := CheckVideoFormat(EXT_MPEG);
      TagParser.IncludeWMVTags := CheckVideoFormat(EXT_WMV);
    except
      // Never mind... we'll use defaults
    end;
  finally
    Free;
  end;
end;

function TAHMFormatPreferences.ParseXML(const Filepath: String): Boolean;
var
  RootNode, NextNode: TAHMXMLNode;
begin
  // Default result - problem loading Preferences file
  Result := False;

  // Don't bother trying to parse XML file if its missing
  if not FileExists(FilePath + PREFS_FILENAME) then Exit;

  FHelper := TAHMXMLHelper.Create(Filepath + PREFS_FILENAME);
  try
    // Ignore preamble - we're looking for a ClientPreferences node
    if not FHelper.FindRootElement(RootNode, TAG_PREFERENCES) then Exit;

    // What nodes do we have here?
    NextNode := nil;
    while FHelper.NextElement(RootNode, NextNode) do
    begin
      // Ignore anything but video & audio formats and proxy settings
      if FHelper.CurrentElement = TAG_EXTNS_VIDEO then
        FVideoFormats.CommaText := FHelper.GetValue
      else if FHelper.CurrentElement = TAG_EXTNS_AUDIO then
        FAudioFormats.CommaText := FHelper.GetValue
      else if FHelper.CurrentElement = TAG_PROXY_HOST then
        InternetHelperInstance.ProxyServer := FHelper.GetValue
      else if FHelper.CurrentElement = TAG_PROXY_PORT then
        InternetHelperInstance.ProxyPortAsString := FHelper.GetValue
      else if FHelper.CurrentElement = TAG_PROXY_USER then
        InternetHelperInstance.ProxyUsername := FHelper.GetValue
      else if FHelper.CurrentElement = TAG_PROXY_PASS then
        InternetHelperInstance.ProxyPassword := FHelper.GetValue;
    end;

    // All is well, parsed preferences file successfully
    Result := True;
  finally
    FHelper.Free;
  end;
end;

function TAHMFormatPreferences.CheckVideoFormat(Extension: String): Boolean;
begin
  Result := FVideoFormats.IndexOf(Extension) >= 0;
end;

function TAHMFormatPreferences.CheckAudioFormat(Extension: String): Boolean;
begin
  Result := FAudioFormats.IndexOf(Extension) >= 0;
end;

end.
